package myShoppingCart

import (
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/controllers/login"
	USER "OnlineRetailers/models/User"
	error2 "OnlineRetailers/models/errorDeal"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type DeleteMyShoppingCart struct {
	beego.Controller
}

func (c *DeleteMyShoppingCart) DeleteMyShoppingCart() {

	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/myShoppingCart/DeleteMyShoppingCart"
	shoppingCarts := []*USER.ShoppingCart{}
	//shoppingCart := &USER.ShoppingCart{}
	//user := []*Admin.User{}
	log := &error2.Log{}
	mes := &error2.Mes{}
	var err error
	o := orm.NewOrm()

	fmt.Println(c.GetString("shoppingCart"))
	err = json.Unmarshal([]byte(c.GetString("shoppingCart")), &shoppingCarts)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	err, _ = login.CheckOpenid(c.Ctx.Request.Header["Openid"])

	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	fmt.Println(shoppingCarts)
	ch := make(chan int)
	go func() {
		for _, shoppingCart := range shoppingCarts {
			//class:=Admin.Classification{ClassificationId: classification.ClassificationId}
			num, err := o.Delete(shoppingCart)
			fmt.Println(num)
			if err != nil {
				ch <- -1
				return

			}
			ch <- 1
		}
	}()

	for range shoppingCarts {

		if <-ch == -1 {
			c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
			return

		}
	}

	mes.Mes = "success"
	mes.Code = "200"
	c.Data["json"] = mes
}
